package com.week.tiny_video.controller;

import com.week.tiny_video.pojo.Category;
import com.week.tiny_video.result.Result;
import com.week.tiny_video.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
@CrossOrigin(allowCredentials = "true")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("category/{id}")
    public Result get(@PathVariable("id") Integer id){
        Category category = categoryService.get(id);
        HashMap<String,Object> map = new HashMap<>();
        map.put("category",category);
        return new Result(200,"所有分类",true,map);
    }

    //添加分类
    @PostMapping("category")
    public Result add(@RequestBody Category category){
        categoryService.add(category);
        return new Result(200,"添加成功",true);
    }

    //分类修改
    @PutMapping("category")
    public Result update(@RequestBody Category category){
        categoryService.update(category);
        return new Result(200,"修改成功",true);
    }

    //删除分类
    @DeleteMapping("category/{id}")
    public Result delete(@PathVariable("id") Integer id){
        categoryService.delete(id);
        return new Result(200,"删除成功",true);
    }

    //所有分类列表
    @GetMapping("category/list")
    public Result list(){
        List<Category> list = categoryService.list();
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",list);
        return new Result(200,"所有分类",true,map);
    }
}
